// pages/logistics/index.js
import Router from '../../utils/router.js';
import base from '../../config/base.js';
import orderlogisticsService from '../../services/orderlogistics.js';
var courier_number = '',
  express_company = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoflag: false,
    goods_img: base.static + '/my/morentouxiang@2x.png',
    courier_number: '',
    express_company: '',
    list: [],
    dialphone: {
      isShow: false,
      phone: '',
      animate: false
    },
    lineparams: 0,
    run_num: '',
  },
  /**
   * 复制邀请码 
   */
  copyCode: function () {
    let c = this.data.courier_number + '' || this.data.info.no;
    wx.setClipboardData({
      data: c,
      success(res) {
        wx.showToast({
          title: '复制成功',
        });
      },
      fail(res) {
        wx.showToast({
          title: '复制失败',
          icon: 'none'
        });
      },
    })
  },
  /**
   * 点击 电话\订单号 出现弹框
   */
  phoneCall: function (e) {
    var that = this,
      dialphone = that.data.dialphone,
      time = 200;
    dialphone.isShow = true;
    dialphone.numString = e.currentTarget.dataset.replyPhone;
    dialphone.animate = false;
    that.setData({
      dialphone: dialphone
    });
    setTimeout(function () {
      dialphone.animate = true;
      that.setData({
        dialphone: dialphone
      });
    }, time);
  },
  loadimg: function (e) {
    var hArr = [];
    // console.log(e.target);
  },
  getlogistics: function (res) {
    var that = this,
      infoflag = true,
      _height = 0,
      list = that.data.list;
    for (let index in list) {
      var model = list[index];
      var info = model.context;
      var infoclone = model.context;
      var phonelist = info.match(/((((13[0-9])|(15[^4])|(18[0,1,2,3,5-9])|(17[0-8])|(147))\d{8})|((\d3,4|\d{3,4}-|\s)?\d{7,14}))?/g);
      var phone_idx = -1;
      for (let i in phonelist) {
        if (phonelist[i] != '') {
          phone_idx++;
          model['phone' + phone_idx] = {
            index: i,
            phone: phonelist[i]
          };
        }
      }
      if (phone_idx >= 0) {
        model.list = [];
        for (let i = 0; i <= phone_idx; i++) {
          let tar = infoclone.split(model['phone' + i].phone);
          infoclone = infoclone.split(model['phone' + i].phone)[1];
          if (tar[0] != '') {
            model.list.push({
              type: 'txt',
              txt: tar[0]
            });
            model.list.push({
              type: 'phone',
              txt: model['phone' + i].phone
            })
          } else {
            model.list.push({
              type: 'phone',
              txt: model['phone' + i].phone
            })
            model.list.push({
              type: 'txt',
              txt: tar[0]
            });
          }
        }
        if (infoclone != '') {
          model.list.push({
            type: 'txt',
            txt: infoclone
          })
        }
      }
    }
    that.setData({
      list
    });
    that.queryMultipleNodes();
  },
  /**
   * 获取屏幕宽度
   */
  systemInfo: function () {
    var that = this;
    wx.getSystemInfo({
      success: res => {
        that.getlogistics(res);
      },
      fail: err => {
        that.systemInfo();
      }
    })
  },
  /**
     * 获取物流
     */
  queryorderlogistics: function () {
    var that = this;
    let param = {
      courier_number: courier_number,
      express_company: express_company
    };
    orderlogisticsService.queryorderlogistics(param, res => {
      if (res && res.code == 1) {
        let info = res.data,
          list = info;
        that.setData({
          info, list
        });
        that.systemInfo();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    });

  },
  //西集的物流追踪
  gettrackslogisticsxiji(){
    let that = this;
    let param = {
      run_num: that.data.run_num
    }
    orderlogisticsService.trackslogisticsxiji(param,function(res){
      if(res && res.code == 1){
        let data = res.data.data.agent_list[0];
        that.setData({
          express_company: data.company,
          courier_number: data.tracking_number,
          list: data.logisticsInformation.reverse()
        })
        that.systemInfo();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      infoflag = true;
    if (options.courier_number && options.courier_number != 'null') {
      courier_number = options.courier_number;
    }
    if (options.express_company && options.express_company != 'null') {
      express_company = options.express_company;
    }
    if (options.goods_img && options.goods_img != 'null') {
      var goods_img = options.goods_img;
      that.setData({
        goods_img
      });
    }
    if (options.run_num){
      that.setData({
        run_num: options.run_num
      })
    }
    that.setData({
      courier_number, express_company, infoflag
    });
    if (options.srlnid == 751){
      that.gettrackslogisticsxiji();      
    }else {
      that.queryorderlogistics();      
    }
  },
  queryMultipleNodes: function () {
    var that = this;
    wx.createSelectorQuery().select('.logistics-single.active .spot').boundingClientRect(function (start) {
      var t1 = start.top || 0;
      wx.createSelectorQuery().select('.logistics-single.end .spot').boundingClientRect(function (end) {
        var t2 = end.top || 0;
        that.setData({
          lineparams: t2 - t1
        });
        // rect.id      // 节点的ID
        // rect.dataset // 节点的dataset
        // rect.left    // 节点的左边界坐标
        // rect.right   // 节点的右边界坐标
        // rect.top     // 节点的上边界坐标
        // rect.bottom  // 节点的下边界坐标
        // rect.width   // 节点的宽度
        // rect.height  // 节点的高度
      }).exec()
      // rect.id      // 节点的ID
      // rect.dataset // 节点的dataset
      // rect.left    // 节点的左边界坐标
      // rect.right   // 节点的右边界坐标
      // rect.top     // 节点的上边界坐标
      // rect.bottom  // 节点的下边界坐标
      // rect.width   // 节点的宽度
      // rect.height  // 节点的高度
    }).exec()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
